package com.yuji.aacar.controller.webview;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * <li>Package:com.yuji.aacar.controller.webview</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/3/23</li>
 * <li>Description:   </li>
 */

public class WebViewActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.back_image)
    AutoRelativeLayout backImage;
    @BindView(R.id.title_content)
    PingFangTextView titleContent;
    @BindView(R.id.back_login)
    PingFangTextView backLogin;
    @BindView(R.id.title_message_iv)
    ImageView titleMessageIv;
    @BindView(R.id.title_message_rl)
    AutoRelativeLayout titleMessageRl;
    @BindView(R.id.title_rl)
    AutoRelativeLayout titleRl;
    @BindView(R.id.webView)
    WebView webView;

    private String url;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);
        initTitle(true, "活动详情", null);

        url = getIntent().getStringExtra("url");

        super.onCreate(savedInstanceState);if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setSupportZoom(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setBlockNetworkImage(false);
        // webSettings.setDomStorageEnabled(true);
        //webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showLoadingDialog();

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dismissLoadingDialog();
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }


        });

        Intent intent = getIntent();
        if (intent != null) {
//            carDetailTitle.setCenterText(intent.getStringExtra("title"));
//            auctionId = intent.getStringExtra("auctionId");
        }

        if (null != url){
            webView.loadUrl(url);
        }

    }


//    @Override
//    public void requestSuccess(CoreServiceResponse response) {
//        super.requestSuccess(response);
//
//        webView.loadDataWithBaseURL("https://cdn20.ttpaicdn.com/",response.getResponse()+"", "text/html", "utf-8",null);
//
//
//        Log.e("carDetial:==", HttpConfig.WEB_URL + HttpConfig.WEB_HEAD_MAP.toString());
//    }


    @Override
    public void requestFailed(CoreServiceResponse response) {
        super.requestFailed(response);
        dismissLoadingDialog();
    }

    @Override
    public void onResume() {
        super.onResume();
//        MobclickAgent.onResume(this);
        webView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
//        MobclickAgent.onPause(this);
        webView.onPause();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();
    }

}
